<template>
    <div style="position: relative;font-size: .14rem;overflow: hidden">
        <div style="position: relative;margin-top: .2rem">
            <div style="width: 100%;position:absolute;display:flex;bottom: .1rem;left: 0;z-index: 2">
                <transition enter-active-class="animated bounceInLeft" leave-active-class="animated bounceOutLeft">
                    <div v-show="peopleA" style="width: 35%;"><img :src="roleImgA" style="width: 100%"></div>
                </transition>
                <transition enter-active-class="animated bounceIn" leave-active-class="animated bounceOut">
                    <div v-show="chatA" class="bubble-A">
                        <div>{{contentA}}</div>
                    </div>
                </transition>
                <transition enter-active-class="animated bounceIn" leave-active-class="animated bounceOut">
                    <div v-show="thinkA" class="bubble-A">
                        <div>.......<i class="icon iconfont icon-luyin icon-btn"></i></div>
                    </div>
                </transition>
            </div>
            <transition enter-active-class="animated bounceInDown" leave-active-class="animated bounceOutUp">
                <div v-show="scenario" style="z-index: 1"><img :src="sceneImg" style="width: 100%"></div>
            </transition>
            <div style="width: 100%;position:absolute;top: 0;right: 0;z-index: 2">
                <transition enter-active-class="animated bounceInRight" leave-active-class="animated bounceOutRight">
                    <div v-show="peopleB" style="width: 35%;float: right">
                        <img :src="roleImgB" style="width: 100%"></div>
                </transition>
                <transition enter-active-class="animated bounceIn" leave-active-class="animated bounceOut">
                    <div v-show="chatB" class="bubble-B">
                        <div v-html="contentB"></div>
                    </div>
                </transition>
            </div>
        </div>

        <div class="input-content">
            <input class="input-msg" id="inputmsg" v-model="dialogValue" type="text">
            <div class="determine" style="" @click="confirmDialog()">发送</div>
        </div>

        <!--红包图片动画-->
        <transition name="fade">
            <div class="draw-gift" v-show="drawShow">
                <div class="draw-img">

                    <div class="draw-mid draw-mid-move" @click="openRedPacket()"></div>
                    <div class="draw-down"></div>


                    <div class="draw-up-up" id="draw-up-up"></div>
                    <div class="draw-up"></div>
                    <div class="draw-down2"></div>
                    <div class="draw-list"></div>
                </div>
            </div>
        </transition>

    </div>
</template>

<script>
    import {getData} from "../../../config/getData"
    import {mapState, mapMutations} from 'vuex';

    export default {
        data() {
            return {
                peopleA: false,
                peopleB: false,
                scenario: false,
                chatA: false,
                chatB: false,
                thinkA: false,
                dialog: false,
                dialogValue: '',
                priority: '',
                contentA: '',
                contentB: '',
                sceneIndex: 0,
                chatIndex: 0,
                roleImgA: '',
                roleImgB: '',
                sceneImg: '',
                scenarioName: '',
                chatData: {},
                allData: null,
                tips: '',
                partid: null,
                data: {
                    "reqType": 0,
                    "perception": {
                        "inputText": {
                            "text": ""
                        }
                    },
                    "userInfo": {
                        "apiKey": "fa3b2e1a02834d2ca723523944fe97da",
                        "userId": "huqiqi"
                    }
                },
                drawShow: false
            }
        },
        components: {},
        created() {
            /*this.partid = this.$route.query.partid;*/
            this.partid = 0;
        },
        mounted() {
            this.GET_SEX();
            this.GET_START();
            this.initData();

            $("#inputmsg").blur(function () {
                window.scroll(0, 0);
                /*setTimeout(function() {
                    var scrollHeight = document.documentElement.scrollTop || document.body.scrollTop || 0;
                    window.scrollTo(0, Math.max(scrollHeight - 1, 0));
                }, 300);*/
            })
        },
        computed: {
            ...mapState([
                'chatArr', 'sex', 'start'
            ]),
        },
        watch: {},
        methods: {
            ...mapMutations([
                'ADD_CHATLIST', 'REMOVE_CHATLIST', 'SAVE_CHATLIST', 'GET_SEX', 'SAVE_START', 'GET_START'
            ]),
            initData() {
                let info = getData(this.partid);
                if (!info) {
                    this.$router.push('/home/selectMenu');
                    this.$toast.show({text: "无数据，不可访问", time: 1500});
                    return;
                }
                this.allData = info.plot;
                if (this.allData) {
                    this.dealData();
                } else {
                    this.$toast.show({text: "无数据，不可访问", time: 1500});
                }
            },
            dealData() {
                if (!this.sex) {
                    this.$toast.show({text: "请重新选择性别", time: 1500});
                    this.$router.push('/home/selectSex');
                    return;
                }
                let sceneData = this.allData[this.sceneIndex];
                this.roleImgA = require('@/assets/image/scenario-1/' + sceneData.roleA + '.png');
                this.roleImgB = require('@/assets/image/scenario-1/' + sceneData.roleB + '.png');
                this.sceneImg = require('@/assets/image/scenario-1/' + sceneData.scene + '.png');
                this.scenarioName = sceneData.scenario;
                this.changeSceneShow();
                /*  if(this.start == "true") {
                      setTimeout(() => {
                          this.chatAction();
                      }, 1000);
                      setTimeout(() => {
                          this.drawShow = true;
                      }, 4000);
                  } else {*/
                this.data.perception.inputText.text = "讲笑话";
                this.chatRequire(this.data);
                //角色A交谈动画
                setTimeout(() => {
                    if (this.chatA) {
                        this.chatA = false;
                        setTimeout(() => {
                            this.thinkA = true;
                        }, 1000)
                    } else {
                        this.thinkA = true;
                    }
                }, 1500)
                /*}*/
            },
            chatAction() {
                console.log(this.chatArr);
                let sceneData = this.allData[this.sceneIndex];
                this.chatData = sceneData.dialog[this.chatIndex];
                if (this.chatData) {
                    this.tips = this.chatData.tips;
                    let val = this.chatData.priority;
                    if (val == 'A') {
                        if (this.chatA) {
                            this.chatA = false;
                            setTimeout(() => {
                                this.thinkA = true;
                            }, 1000)
                        } else {
                            this.thinkA = true;
                        }
                    } else {
                        //角色B交谈动画
                        if (this.chatB) {
                            this.chatB = false;
                            setTimeout(() => {
                                this.contentB = this.chatData.npc;
                                this.chatB = true;
                            }, 1000)
                        } else {
                            this.contentB = this.chatData.npc;
                            this.chatB = true;
                        }

                        //角色A交谈动画
                        setTimeout(() => {
                            if (this.chatA) {
                                this.chatA = false;
                                setTimeout(() => {
                                    this.thinkA = true;
                                }, 1000)
                            } else {
                                this.thinkA = true;
                            }
                        }, 1500)
                    }
                } else {
                    this.$toast.show({text: "聊天数据异常", time: 1500});
                    return;
                }
            },
            changeSceneHide() {
                this.thinkA = false;
                this.chatB = false;
                this.chatA = false;
                setTimeout(() => {
                    this.peopleA = false;
                    this.peopleB = false;
                    setTimeout(() => {
                        this.scenario = false;
                    }, 300);
                }, 500);
            },
            changeSceneShow() {
                this.scenario = true;
                setTimeout(() => {
                    this.peopleA = true;
                    this.peopleB = true;
                }, 500);
            },
            showDialog() {
                this.dialog = true;
            },
            cancelDialog() {
                this.dialog = false;
            },
            confirmDialog() {
                if (!this.dialogValue) {
                    this.$toast.show({text: "提交对话不能为空哟", time: 1500});
                    return;
                }
                let dialog = this.dialogValue;
                if (dialog.indexOf("重置") > -1) {
                    this.SAVE_START("true");
                    this.$router.push('/home/progress');
                    return;
                }

                /*if (this.start == "true") {
                    if (dialog.indexOf("想") > -1) {
                        this.SAVE_START("false");
                    } else if(dialog.indexOf("游戏")) {
                        this.dialogValue = '';
                        this.thinkA = false;
                        setTimeout(() => {
                            this.contentA = dialog;
                            this.chatA = true;
                        }, 1000)

                        this.chatB = false;
                        setTimeout(() => {
                            this.contentB = "小迷弟正在为你跳转到琪琪游戏厅，有很多好玩有趣的游戏在等着你哟！";
                            this.chatB = true;
                        }, 2000)
                        setTimeout(() => {
                            window.location.href = "http://dlcblog.com/wxgame/";
                        }, 4000)
                        return;
                    } else {
                        this.dialogValue = '';
                        this.thinkA = false;
                        setTimeout(() => {
                            this.contentA = dialog;
                            this.chatA = true;
                        }, 1000)

                        this.chatB = false;
                        setTimeout(() => {
                            this.contentB = "想不想知道呢？";
                            this.chatB = true;
                        }, 2000)
                        setTimeout(() => {
                            this.thinkA = true;
                            this.chatA = false;
                        }, 3000)
                        return;
                    }
                }*/

                if(dialog.indexOf("游戏") > -1) {
                    this.dialogValue = '';
                    this.thinkA = false;
                    setTimeout(() => {
                        this.contentA = dialog;
                        this.chatA = true;
                    }, 1000)

                    this.chatB = false;
                    setTimeout(() => {
                        this.contentB = "小迷弟正在为你跳转到琪琪游戏厅，有很多好玩有趣的游戏在等着你哟！";
                        this.chatB = true;
                    }, 2000)
                    setTimeout(() => {
                        window.location.href = "http://dlcblog.com/wxgame/";
                    }, 5000)
                    return;
                }

                this.dialog = false;
                this.data.perception.inputText.text = dialog;
                this.thinkA = false;
                setTimeout(() => {
                    this.contentA = dialog;
                    this.chatA = true;
                }, 1000)
                this.chatRequire(this.data);
            },
            chatRequire(data) {
                let _this = this;
                this.dialogValue = '';
                this.$http.post('/openapi/api/v2', data).then(function (result) {
                    let chatcontent = result.data.results[0].values.text;
                    if (chatcontent) {
                        if (chatcontent.indexOf("请求次数") > -1) {
                            chatcontent = "5555,俺累了，找哥哥去聊吧！~(^o^)~";
                        } else {
                            if(_this.data.perception.inputText.text == "讲笑话") {
                                chatcontent = "小迷弟我讲个笑话吧！~(^o^)~" + chatcontent;
                            }
                        }
                        _this.chatB = false;
                        setTimeout(() => {
                            _this.contentB = chatcontent;
                            _this.chatB = true;
                        }, 2000)
                        setTimeout(() => {
                            _this.thinkA = true;
                            _this.chatA = false;
                        }, 3000)
                    }
                }).catch(function (error) {
                    _this.chatB = false;
                    setTimeout(() => {
                        _this.contentB = "聊天室出现故障了，赶快找哥哥来修理，55555";
                        _this.chatB = true;
                    }, 1000)
                    _this.$toast.show({text: '网络波动', time: 2500});
                })
            },
            addChatAction() {
                this.ADD_CHATLIST({
                    priority: this.priority,
                    contentA: this.contentA,
                    contentB: this.contentB,
                    scenarioName: this.scenarioName,
                    sceneIndex: this.sceneIndex,
                    chatIndex: this.chatIndex
                });
            },
            viewChat() {
                let _this = this;
                this.$vux.confirm.show({
                    title: '提示',
                    content: '确认提交聊天数据吗？',
                    onConfirm() {
                        _this.SAVE_CHATLIST(_this.partid);
                        _this.$router.push({
                            path: '/home/record',
                            query: {
                                partid: _this.partid,
                            }
                        })
                    }
                })

            },
            openRedPacket() {
                $(".draw-mid").hide();

                $(".draw-up").animate({
                    opacity: "1"
                }, 100);
                $(".draw-up-up").animate({
                    opacity: "1",
                    top: "-1rem"
                }, 200);


                $(".draw-down").animate({
                    opacity: "0"
                }, 300);
                $(".draw-down2").animate({
                    opacity: "1"
                }, 300);
                $(".draw-list").animate({opacity: "1"}, 600).animate({top: "-0.3rem"}, 1000);

                setTimeout(() => {
                    this.chatIndex = 1;
                    this.chatAction();
                    this.drawShow = false;
                }, 3000)

                setTimeout(() => {
                    this.chatIndex = 2;
                    this.chatAction();
                }, 7000)
            }
        }
    }
</script>

<style lang="less" rel="stylesheet/less">

    @keyframes button {
        0% {
            transform: scale(1);
        }
        50% {
            transform: scale(0.7);
        }
        100% {
            transform: scale(1);
        }
    }

    .top-nav {
        position: relative;
        width: 100%;
        top: 0;
        left: 0;
        background: rgba(219, 216, 216, 0.9);
        text-align: center;
        font-size: .14rem;
        z-index: 6
    }

    .bubble-A {
        position: absolute;
        right: 0;
        background: rgba(230, 229, 230, 0.93);
        width: 60%;
        margin: .2rem .2rem .1rem 0;
        border-radius: .05rem;
        padding: .05rem;
        z-index: 3
    }

    .bubble-A::before {
        content: '';
        content: '';
        position: absolute;
        width: 0;
        height: 0;
        top: .1rem;
        left: -.1rem;
        border-width: .06rem;
        border-style: solid;
        border-color: transparent rgba(230, 229, 230, 0.93) transparent transparent;
        line-height: 0;
        font-size: 0;
    }

    .bubble-B {
        position: absolute;
        left: 0;
        background: rgba(230, 229, 230, 0.93);
        width: 60%;
        margin: .2rem 0rem 0rem .2rem;
        border-radius: .05rem;
        padding: .05rem;
        z-index: 3
    }

    .bubble-B::before {
        content: '';
        content: '';
        position: absolute;
        width: 0;
        height: 0;
        top: .1rem;
        right: -.11rem;
        border-width: .06rem;
        border-style: solid;
        border-color: transparent transparent transparent rgba(230, 229, 230, 0.93);
        line-height: 0;
        font-size: 0;
    }

    .dialog-content {
        position: absolute;
        top: 30%;
        left: 0;
        width: 100%;
        z-index: 4;
        .dialog-text {
            width: 2.5rem;
            height: 1rem;
            margin: 0 auto;
            .dialog-value {
                width: 100%;
                height: 100%;
                font-size: .15rem;
                border: .01rem solid #666;
                background: rgba(230, 229, 230, 0.95);
            }
            .dialog-btn {
                display: flex;
                text-align: center;
                align-items: center;
                height: .3rem;
                background: rgba(230, 229, 230, 0.95);
                font-size: .18rem;
                border: .01rem solid #666;
                border-top: 0;
                border-radius: .05rem;
                margin-top: -.05rem;
                .dialog-confirm {
                    flex: 1;
                    border-right: .01rem solid #666;
                    color: green;
                }
                .dialog-cancel {
                    flex: 1;
                    color: #666;
                }
            }
        }
    }

    .determine {
        width: 20%;
        background-color: #30cd4b;
        font-size: .15rem;
        color: #fff;
        text-align: center;
        margin: 0 .08rem;
        line-height: .34rem;
        border-radius: 0.03rem;
    }

    .input-content {
        position: fixed;
        display: flex;
        bottom: 0;
        left: 0;
        height: .5rem;
        width: 100%;
        align-items: center;
        background: rgba(214, 214, 214, 0.81);
        z-index: 8;
    }

    .input-msg {
        border-radius: 0.03rem;
        width: 75%;
        height: 0.35rem;
        margin-left: 0.08rem;
        font-size: .15rem;
        padding: 0 .1rem;
    }

    .icon-btn {
        font-size: .2rem;
        float: right;
        animation: button 1s infinite ease-in-out;
    }

    .draw-gift {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 20;
    }

    .draw-gift .draw-img {
        position: relative;
        width: 2rem;
        margin: 1.5rem auto;
    }

    .draw-gift .draw-img .draw-down {
        width: 2rem;
        height: 2rem;
        background-position: center;
        background-repeat: no-repeat;
        background-size: 100% 100%;
        background-image: url(../../../assets/image/redpacket/open-no.png);
        z-index: 30;
        /*-webkit-animation:draw-down 0.5s ease 1.2s 1 alternate;*/
        /*-webkit-animation-timing-function:cubic-bezier(0.25,0.1,0.25,1);*/
    }

    .draw-gift .draw-img .draw-mid {
        position: absolute;
        width: 1rem;
        height: 1rem;
        background-position: center;
        background-repeat: no-repeat;
        background-size: 100% 100%;
        background-image: url(../../../assets/image/redpacket/open.png);
        z-index: 40;
        top: 0.2rem;
        left: 25%;
        /*animation:open 0.3s linear 1s 3 alternate;*/
    }

    .draw-gift .draw-img .draw-down2 {
        position: absolute;
        top: 0;
        width: 2rem;
        height: 2rem;
        background-position: center;
        background-repeat: no-repeat;
        background-size: 100% 100%;
        background-image: url(../../../assets/image/redpacket/open-before.png);
        z-index: 32;
        opacity: 0;
        /*-webkit-animation:draw-down2 0.5s ease 1.4s 1 alternate;*/
        /*-webkit-animation-timing-function:cubic-bezier(0.25,0.1,0.25,1);*/
    }

    .draw-gift .draw-img .draw-up-up {
        position: absolute;
        width: 2rem;
        height: 1rem;
        background-position: center;
        background-repeat: no-repeat;
        background-size: 100% 100%;
        background-image: url(../../../assets/image/redpacket/open-back-2.png);
        top: 0rem;
        opacity: 0;
        z-index: 32;
    }

    .draw-gift .draw-img .draw-up {
        position: absolute;
        width: 2rem;
        height: 1rem;
        background-position: center;
        background-repeat: no-repeat;
        background-size: 100% 100%;
        background-image: url(../../../assets/image/redpacket/open-back-1.png);
        top: 0;
        z-index: 33;
        opacity: 0;
    }

    .draw-gift .draw-img .draw-list {
        position: absolute;
        width: 1rem;
        height: 1rem;
        background-position: center;
        background-repeat: no-repeat;
        background-size: 100% 100%;
        background-image: url(../../../assets/image/master.jpg);
        top: 0.2rem;
        left: 25%;
        opacity: 0;
        z-index: 35;
    }

    @keyframes draw-down {
        0% {
            opacity: 1;
        }
        50% {
            opacity: 0.5;
        }
        100% {
            opacity: 0;
        }
    }

    @keyframes draw-down2 {
        0% {
            opacity: 0;
        }
        50% {
            opacity: 0.5;
        }
        100% {
            opacity: 1;
        }
    }

    .draw-gift .draw-img .draw-mid-move {
        -webkit-animation: open 0.2s linear 0.5s infinite alternate;
        -webkit-animation-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
        animation: open 0.2s linear 0.5s infinite alternate;
        animation-timing-function: cubic-bezier(0.25, 0.1, 0.25, 1);
    }

    @keyframes open {
        0% {
            transform: scale(1);
        }
        100% {
            transform: scale(0.9);
        }
    }

    .fade-enter-active, .fade-leave-active {
        transition: opacity .5s;
    }

    .fade-enter, .fade-leave-active {
        opacity: 0;
    }
</style>
